﻿@using WikeSoft.Data.Interfaces
@using WikeSoft.Data.Models
@model  WikeSoft.Core.AttachmentSet

@{

    var attachmentService = DependencyResolver.Current.GetService<IAttachmentService>();
    List<AttachmentModel> attachments = new List<AttachmentModel>();
    if (Model.ObjectId.HasValue)
    {
        attachments = attachmentService.GetAttachmentsByObject(Model.ObjectId.Value, Model.GroupCode);
    }


}


<table class="table table-hover">
    <colgroup>
        <col width="auto">
        <col width="40px;">
        <col width="40px;">
    </colgroup>
    @for (int i = 0; i < attachments.Count; i++)
    {
        var responseData = attachments[i];
        if (responseData.IsImg)
        {
            <tr>
                <td><input class="attachment" type="hidden" name ="@Model.FiledName" value="@responseData.Id"/><a href="@responseData.UrlPath" target="_blank">@responseData.TrueName<img title="@responseData.TrueName" src="@responseData.ThumUrlPath" class="image-thum" style="width: 50px"/></a></td>
                <td><a target="_blank" href="/Attachment/Download?Id=@responseData.Id">下载</a></td>
                @if (Model.HaveRemove)
                {
                    <td><a href="#" onclick="delAttachment(this)">删除</a></td>
                }

            </tr>

        }
        else
        {
            <tr>
                <td><input class="attachment" type="hidden" name ="@Model.FiledName" value="@responseData.Id"/><a href="@responseData.UrlPath" target="_blank">@responseData.TrueName</a></td>
                <td><a target="_blank" href="/Attachment/Download?Id=@responseData.Id">下载</a></td>
                @if (Model.HaveRemove)
                {
                    <td><a href="#" onclick="delAttachment(this)">删除</a></td>
                }
            </tr>


        }
    }

</table>

<script type="text/javascript">
    //上传成功后的回调方法
    //loadId：上传控件的ID
    //responseData：上传成功后的响应数据
    function callBackData(loadId, responseData,fieldname) {
        if (responseData.IsImg) {
            $('<tr><td><input class="attachment" type="hidden" name="' + fieldname + '" value="' + responseData.Id + '" /><a href="' + responseData.UrlPath + '" target="_blank">' + responseData.TrueName + '<img  title="' + responseData.TrueName + '"  src="' + responseData.ThumUrlPath + '" style="width: 50px"  class="image-thum"/></a></td><td><a target="_blank" href="/Attachment/Download?Id=' + responseData.Id + '">下载</a></td><td><a href="#" onclick="delAttachment(this)">删除</a></td></tr>').appendTo($(loadId).next());
        } else {
            $('<tr><td><input class="attachment" type="hidden" name="' + fieldname + '" value="' + responseData.Id + '" /><a href="' + responseData.UrlPath + '" target="_blank">' + responseData.TrueName + '</a></td><td><a target="_blank" href="/Attachment/Download?Id=' + responseData.Id + '">下载</a></td><td><a href="#" onclick="delAttachment(this)">删除</a></td></tr>').appendTo($(loadId).next());
        }
    }

    function delAttachment(dom) {
        var id = $(dom).parent().parent().find("input:hidden").val();
      
        $.ajax({
            url: '/Attachment/DeleteAttachment',
            type: "POST",
            dataType: "json",
            data: JSON.stringify({ id: id }),
            contentType: "application/json, charset=utf-8",
            success: function (data) {
                
                if (data.success) {
                    $(dom).parent().parent().remove();
                } else {
                    top.layer.alert("删除失败：" + data.message);
                }
            }
        });
       
    }
</script>